#include "./../inc/file.h"
#include "./../inc/lcd.h"
#include "./../inc/touch.h"
#include "log.h"

// char file_path[256] = "/home/naim/res";
char file_path[256] = "/root/res";
int currIMG = 0; // 当前显示的图片
int prevIMG = 0; // 上一次显示的图片

FILE *openLog(); // 日志

int main()
{
    FILE *fp_log = openLog();                   // 日志
    FILE *fp_touch = touch_init();              // 初始化触摸屏
    FILE *fp_lcd = lcd_init();                  // 初始化LCD
    LCDBuffer lcd_mmap = lcd_mmap_init(fp_lcd); // 映射LCD
    file_node_p headNode = file_node_init();    // 初始化文件节点

    findBMPByDir(file_path, headNode);          // 找到指定目录下的所有图片,并保存在链表中
    int IMGCount = get_file_node_num(headNode); // 获取图片数量

    show_bmp(headNode, currIMG, lcd_mmap, NONE, NONE_CLICK, IMGCount); // 显示第一张图片
    //=======================================================

    int direction = NONE;        // 初始化方向
    int touch_type = NONE_CLICK; // 初始化触摸事件
    while (1)
    {
        // 重置方向和触摸事件
        direction = NONE;
        touch_type = NONE_CLICK; 
        // selete 检测时间 设置3秒超时
        struct timeval tv;
        tv.tv_sec = 3;
        tv.tv_usec = 0;

        // 初始化文件描述符集合
        fd_set fds;
        FD_ZERO(&fds);                  // 清空集合
        FD_SET(fileno(fp_touch), &fds); // 将触摸屏文件描述符加入集合

        int ret = select(fileno(fp_touch) + 1, &fds, NULL, NULL, &tv);

        if (ret > 0) // 检测到触摸屏数据
        {
            get_touch_gesture(fp_touch, &direction, &touch_type); // 获取触摸屏手势
            if (direction == RIGHT)                               // 向右滑动
                currIMG = (currIMG + 1) % IMGCount;
            else if (direction == LEFT) // 向左滑动
            {
                if (currIMG == 0)
                {
                    currIMG = IMGCount - 1;
                }
                else
                {
                    currIMG = (currIMG - 1) % IMGCount;
                }
            }
        }
        else
        {
            currIMG = (currIMG + 1) % IMGCount;
            direction = RIGHT;
            touch_type = NONE_CLICK;
        }

        if (currIMG != prevIMG)
        {
            show_bmp(headNode, currIMG, lcd_mmap, direction, touch_type, IMGCount);
            prevIMG = currIMG;
        }
    }

    free(headNode);
    free(fp_touch);
    free_lcd(fp_lcd, lcd_mmap);
    fclose(fp_log);
}

// void carousel_indicator(LCDBuffer lcd_mmap, int IMGCount, int currIMG)
// {
//     for (int height = 441; height < 451; height++)
//     {
//         for (int width = 0; width < 800; width++)
//         {
//             int currLength = 600 / (IMGCount + 1);
//             if (width > 149 && width < 649)
//             {
//                 if (width > 149 + (currLength * currIMG) && width < 149 + (currLength * (currIMG + 1)))
//                 {
//                     lcd_mmap[height][width] = BLACK;
//                 }
//                 else
//                 {
//                     lcd_mmap[height][width] = WHITE;
//                 }
//             }
//         }
//     }

// }

FILE *openLog()
{
    FILE *fp = fopen("res/log.txt", "a+");
    if (fp == NULL)
    {
        printf("create log file failed.\n");
        return fp;
    }

    // 设置日志级别(在终端打印)
    // log_set_level(LOG_TRACE);

    // 设置日志级别(在文件中打印)
    log_add_fp(fp, LOG_INFO);

    log_trace("start trace.");
    log_debug("start debug.");
    log_info("start info.");
    log_warn("start warn.");
    log_error("start error.");
    log_fatal("start fatal");

    // 支持参数打印
    log_info("number is %d, string is %s", 10010, "helloword");

    return fp;
}